
from fastapi.params import Param


class ParkAdminErrorCode:
    Success = 0
    InvalidAccountOrPassword = 1
    FileNotFound = 2
    InvalidParam = 3
    EmptyData = 4
    TaskNotFound = 5
    TaskRunning = 6
    NoPermission = 7
    InvalidCarNumber = 8
    SpiderError = 99
    
    InvalidToken = 50008
    ExpiredToken = 50014

    def GetMsg(code : int):
        if code == ParkAdminErrorCode.InvalidAccountOrPassword:
            return '账号或密码错误'
        if code == ParkAdminErrorCode.InvalidToken:
            return '无效的Token'
        if code == ParkAdminErrorCode.ExpiredToken:
            return 'Token已过期'  
        if code == ParkAdminErrorCode.FileNotFound:
            return '文件不存在'  
        if code == ParkAdminErrorCode.InvalidParam:
            return '无效的参数'  
        if code == ParkAdminErrorCode.EmptyData:
            return '没有找到数据'  
        if code == ParkAdminErrorCode.TaskNotFound:
            return '任务不存在'
        if code == ParkAdminErrorCode.TaskRunning:
            return '任务正在运行'
        if code == ParkAdminErrorCode.NoPermission:
            return '没有权限'
        if code == ParkAdminErrorCode.InvalidCarNumber:
            return '无效的车牌号'
        if code == ParkAdminErrorCode.SpiderError:
            return '内部错误'

class ParkAdminException(Exception):
    def __init__(self, code: ParkAdminErrorCode, context=None) -> None:
        super().__init__(code)
        self.code = code
        self.context = context

    def message(self):
        msg = ParkAdminErrorCode.GetMsg(self.code)
        if self.context:
            msg += f" : {str(self.context)}"
        return msg

    def result(self):
        return {"code": self.code, "message": self.message(), "context": str(self.context)}
